From 68c08f3ae3673d36e9db2141f334ba8cdd291a03 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Wed, 13 Dec 2017 11:31:16 -0700 Subject: [PATCH] add support for shape files with non us-ascii filenames. (#148) --- shape.cc | 113 ++++++++++++++++++++++++++++++++++++++++--- test_encoding_latin1 | 36 +++++++++++++- test_encoding_utf8 | 36 +++++++++++++- 3 files changed, 177 insertions(+), 8 deletions(-) diff --git a/shape.cc b/shape.cc index 32282b96e..5f4fde403 100644 --- a/shape.cc +++ b/shape.cc @@ -59,6 +59,107 @@ arglist_t shp_args[] = { ARG_TERMINATOR }; + +/************************************************************************/ +/* SHPOpenGpsbabel() */ +/************************************************************************/ + +SHPHandle SHPAPI_CALL +SHPOpenGpsbabel( const QString& pszLayer, const char * pszAccess ) + +{ + SAHooks sHooks; + +#ifdef SHPAPI_UTF8_HOOKS + SASetupUtf8Hooks( &sHooks ); + return SHPOpenLL( pszLayer.toUtf8().constData(), pszAccess, &sHooks ); +#else + SASetupDefaultHooks( &sHooks ); + return SHPOpenLL( qPrintable(pszLayer), pszAccess, &sHooks ); +#endif + +} + +/************************************************************************/ +/* SHPCreateGpsbabel() */ +/* */ +/* Create a new shape file and return a handle to the open */ +/* shape file with read/write access. */ +/************************************************************************/ + +SHPHandle SHPAPI_CALL +SHPCreateGpsbabel( const QString& pszLayer, int nShapeType ) + +{ + SAHooks sHooks; + +#ifdef SHPAPI_UTF8_HOOKS + SASetupUtf8Hooks( &sHooks ); + return SHPCreateLL( pszLayer.toUtf8().constData(), nShapeType, &sHooks ); +#else + SASetupDefaultHooks( &sHooks ); + return SHPCreateLL( qPrintable(pszLayer), nShapeType, &sHooks ); +#endif + +} + +/************************************************************************/ +/* DBFOpenGpsbabel() */ +/* */ +/* Open a .dbf file. */ +/************************************************************************/ + +DBFHandle SHPAPI_CALL +DBFOpenGpsbabel( const QString& pszFilename, const char * pszAccess ) + +{ + SAHooks sHooks; + +#ifdef SHPAPI_UTF8_HOOKS + SASetupUtf8Hooks( &sHooks ); + return DBFOpenLL( pszFilename.toUtf8().constData(), pszAccess, &sHooks ); +#else + SASetupDefaultHooks( &sHooks ); + return DBFOpenLL( qPrintable(pszFilename), pszAccess, &sHooks ); +#endif + +} + +/************************************************************************/ +/* DBFCreateExGpsbabel() */ +/* */ +/* Create a new .dbf file. */ +/************************************************************************/ + +DBFHandle SHPAPI_CALL +DBFCreateExGpsbabel( const QString& pszFilename, const char* pszCodePage ) + +{ + SAHooks sHooks; + +#ifdef SHPAPI_UTF8_HOOKS + SASetupUtf8Hooks( &sHooks ); + return DBFCreateLL( pszFilename.toUtf8().constData(), pszCodePage , &sHooks ); +#else + SASetupDefaultHooks( &sHooks ); + return DBFCreateLL( qPrintable(pszFilename), pszCodePage , &sHooks ); +#endif + +} + +/************************************************************************/ +/* DBFCreateGpsbabel() */ +/* */ +/* Create a new .dbf file with default code page LDID/87 (0x57) */ +/************************************************************************/ + +DBFHandle SHPAPI_CALL +DBFCreateGpsbabel( const QString& pszFilename ) + +{ + return DBFCreateExGpsbabel( pszFilename, "LDID/87" ); // 0x57 +} + static void dump_fields(void) { @@ -99,12 +200,12 @@ my_rd_init(const QString& fname) { ifname = fname; // TODO: The .prj file can define the the coordinate system and projection information. - ihandle = SHPOpen(qPrintable(fname), "rb"); + ihandle = SHPOpenGpsbabel(fname, "rb"); if (ihandle == NULL) { fatal(MYNAME ": Cannot open shp file %s for reading\n", qPrintable(ifname)); } - ihandledb = DBFOpen(qPrintable(fname), "rb"); + ihandledb = DBFOpenGpsbabel(fname, "rb"); if (ihandledb == NULL) { fatal(MYNAME ": Cannot open dbf file %s for reading\n", qPrintable(ifname)); } @@ -376,13 +477,13 @@ my_write(void) switch (global_opts.objective) { case wptdata: case unknown_gpsdata: - ohandle = SHPCreate(qPrintable(ofname), SHPT_POINT); + ohandle = SHPCreateGpsbabel(ofname, SHPT_POINT); if (ohandle == NULL) { fatal(MYNAME ": Cannot open shp file %s for writing\n", qPrintable(ofname)); } - ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n"); + ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n"); if (ohandledb == NULL) { fatal(MYNAME ": Cannot open dbf file %s for writing\n", qPrintable(ofname)); @@ -392,13 +493,13 @@ my_write(void) break; case rtedata: case trkdata: - ohandle = SHPCreate(qPrintable(ofname), SHPT_ARC); + ohandle = SHPCreateGpsbabel(ofname, SHPT_ARC); if (ohandle == NULL) { fatal(MYNAME ": Cannot open shp file %s for writing\n", qPrintable(ofname)); } - ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n"); + ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n"); if (ohandledb == NULL) { fatal(MYNAME ": Cannot open dbf file %s for writing\n", qPrintable(ofname)); diff --git a/test_encoding_latin1 b/test_encoding_latin1 index 360e60e67..e2aa1c493 100755 --- a/test_encoding_latin1 +++ b/test_encoding_latin1 @@ -74,8 +74,42 @@ filenamebase=test_encoding_file errorcount=`expr $errorcount + 1` fi +# test input file name mangling with shape files + echo "testing input file name with shape files" + rm -f ${TMPDIR}/test_encoding_file* + for ext in cpg dbf prj shp shx + do + cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext} + done + ${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || { + echo "ERROR: The input file name was mangled." + errorcount=`expr $errorcount + 1` + } + +# test output file name mangling with shape files + echo "testing output file name with shape files" + rm -f ${TMPDIR}/test_encoding_file* + ${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp + for ext in cpg dbf shp shx + do + count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l) + if [ $count -lt 1 ]; then + echo "ERROR: The output file name was mangled." + errorcount=`expr $errorcount + 1` + fi + done + rm -f ${TMPDIR}/test_encoding_file* + ${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp + for ext in cpg dbf shp shx + do + count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l) + if [ $count -lt 1 ]; then + echo "ERROR: The output file name was mangled." + errorcount=`expr $errorcount + 1` + fi + done + # TODO: add tests to cover formats that use other open methods. -# for example shape files. else echo "$0 cannot run without the en_US.iso88591 locale." diff --git a/test_encoding_utf8 b/test_encoding_utf8 index 8abe07a66..055538df0 100755 --- a/test_encoding_utf8 +++ b/test_encoding_utf8 @@ -74,8 +74,42 @@ filenamebase=test_encoding_fileαβγ errorcount=`expr $errorcount + 1` fi +# test input file name mangling with shape files + echo "testing input file name with shape files" + rm -f ${TMPDIR}/test_encoding_file* + for ext in cpg dbf prj shp shx + do + cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext} + done + ${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || { + echo "ERROR: The input file name was mangled." + errorcount=`expr $errorcount + 1` + } + +# test output file name mangling with shape files + echo "testing output file name with shape files" + rm -f ${TMPDIR}/test_encoding_file* + ${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp + for ext in cpg dbf shp shx + do + count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l) + if [ $count -lt 1 ]; then + echo "ERROR: The output file name was mangled." + errorcount=`expr $errorcount + 1` + fi + done + rm -f ${TMPDIR}/test_encoding_file* + ${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp + for ext in cpg dbf shp shx + do + count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l) + if [ $count -lt 1 ]; then + echo "ERROR: The output file name was mangled." + errorcount=`expr $errorcount + 1` + fi + done + # TODO: add tests to cover formats that use other open methods. -# for example shape files. else echo "$0 cannot run without the UTF-8 charmap." -- 2.30.2